package com.jack.leetcode.array;

import java.util.Scanner;

/***
 * @author zhangjie
 * @date 2020/3/23 21:13
 */

public class 两数相加 {

    public static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }

    public static void main(String[] args) {
    }

    public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode pre = new ListNode(-1);
        ListNode cur = pre;
        int carry = 0;
        while (l1 != null || l2 != null) {
            int x = l1 == null ? 0 : l1.val;
            int y = l2 == null ? 0 : l2.val;
            int sum = x + y + carry;
            int val = sum % 10;
            carry = sum / 10;
            cur.next = new ListNode(val);

            if(l1 != null){
                l1 = l1.next;
            }
            if(l2 != null){
                l2 = l2.next;
            }
            cur = cur.next;
        }
        if(carry == 1){
            cur.next = new ListNode(1);
        }
        return pre.next;
    }
}
